package com.ksr.logistics.controller;

import com.github.pagehelper.PageInfo;
import com.ksr.logistics.pojo.Result;
import com.ksr.logistics.pojo.Role;
import com.ksr.logistics.pojo.User;
import com.ksr.logistics.service.RoleService;
import com.ksr.logistics.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.web.filter.authc.AnonymousFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @RequestMapping("/login")
    public String login(HttpServletRequest request){
        String shiroLoginFailure = (String) request.getAttribute("shiroLoginFailure");
        if (shiroLoginFailure!=null){
            request.setAttribute("errorMsg", "账号或密码错误,请重新输入");
        }
        return "forward:/login.jsp";
    }

    @RequestMapping("/list")
    @RequiresPermissions("admin:list")
    @ResponseBody
    public PageInfo<User> show(Integer pageNum,Integer pageSize,String keyword){
        List<User> users=userService.selPageUsers(pageNum,pageSize,keyword);
        PageInfo<User> infos=new PageInfo<>(users);
        if (infos.getSize()==0 && infos.getPageNum()>1){
            users=userService.selPageUsers(pageNum-1,pageSize,keyword);
            infos=new PageInfo<>(users);
        }
        return infos;
    }

    @RequestMapping("/delete")
    @RequiresPermissions("admin:delete")
    @ResponseBody
    public Result delete(Integer id){
        int res= userService.deleteById(id);
        Result result=new Result();
        if (1==res){
            result.setCode(1);
            result.setMsg("删除成功!");
        }else{
            result.setCode(0);
            result.setMsg("删除失败,请重试");
        }
        return result;
    }

    @RequestMapping("/deletes")
    @RequiresPermissions("admin:deletes")
    @ResponseBody
    public Result deletes(@RequestParam("ids[]") List<Integer> ids){
        int res= userService.deleteByIds(ids);
        Result result=new Result();
        if (1<=res){
            result.setCode(1);
            result.setMsg("删除成功!");
        }else{
            result.setCode(0);
            result.setMsg("删除失败,请重试");
        }
        return result;
    }

    @RequestMapping("/edit")
    @RequiresPermissions("admin:edit")
    public String adminEdit(Model m,Integer userId){
        if (null!=userId){
            User user=userService.selById(userId);
            m.addAttribute("user",user);
        }
       List<Role> roles=roleService.selRoles();
       m.addAttribute("roles",roles);
       return "adminEdit";
    }

    @RequestMapping("/insert")
    @RequiresPermissions("admin:insert")
    @ResponseBody
    public Result insert(User u){
        Result result=new Result(0,"新增用户失败,请重试!");
        u.setCreateDate(new Date());

        String salt= UUID.randomUUID().toString().substring(0, 5);
        u.setSalt(salt);
        String algorithmName="md5";
        String source=u.getPassword();
        int hashIterations=3;
        String pwd = new SimpleHash(algorithmName, source, salt, hashIterations).toHex();
        u.setPassword(pwd);

        int row=userService.insertUser(u);
        if (1==row){
            result.setCode(1);
            result.setMsg("新增用户成功!");
        }

        return result;
    }

    @RequestMapping("/checkUsername")
    @RequiresPermissions("admin:checkUsername")
    @ResponseBody
    public boolean checkUsername(String username){
        boolean res=true;
        List<User> list=userService.selByUsername(username);
        if (list.size()>0){
            res=false;
        }
        return res;
    }

    @RequestMapping("/update")
    @RequiresPermissions("admin:update")
    @ResponseBody
    public Result update(User u){
        Result result=new Result(0,"修改失败,请重试");

        if (StringUtils.isNotBlank(u.getPassword())){
            String salt= UUID.randomUUID().toString().substring(0, 5);
            u.setSalt(salt);
            String algorithmName="md5";
            String source=u.getPassword();
            int hashIterations=3;
            String pwd = new SimpleHash(algorithmName, source, salt, hashIterations).toHex();
            u.setPassword(pwd);
        }

        int row=userService.updateUser(u);
        if (1==row){
            result.setCode(1);
            result.setMsg("账号信息修改成功!");
        }

        return result;
    }

}
